﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.Account.BLL;
using Yipusoft.SoldierArchives.Model;

public partial class Police_SoldierCardCode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400105"))
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            BindCardCode();

            ddlDepartmentList.DataSource = Department.GetList("Right(DepartmentID,2)='00'");
            ddlDepartmentList.DataTextField = "FullName";
            ddlDepartmentList.DataValueField = "DepartmentID";
            ddlDepartmentList.DataBind();


            if (!String.IsNullOrEmpty(Request["ID"]))
            {
                CardCodeInfo cardCodeInfo = CardCode.GetCardCodeInfoByDepartmentID(Request["ID"]);
                ddlDepartmentList.SelectedValue = cardCodeInfo.DepartmentID;
                ddlDepartmentList.Enabled = false;
                Establishment.Text = cardCodeInfo.Establishment.ToString();
                FronCode.Text = cardCodeInfo.FrontCode;
                StartCode.Text = cardCodeInfo.StartNum;
                EndCode.Text = cardCodeInfo.EndNum;
                ContractEstablishment.Text = cardCodeInfo.ContractEstablishment.ToString();
                CadereEstablishment.Text = cardCodeInfo.CadereEstablishment.ToString();
                hfID.Value = cardCodeInfo.ID.ToString();
                EmFronCode.Text = cardCodeInfo.EmFrontCode;
                EmStartCode.Text = cardCodeInfo.EmStartNum;
                EmEndCode.Text = cardCodeInfo.EmEndNum;
                ContractEstablishment.Text = cardCodeInfo.ContractEstablishment.ToString();

            }
        }
    }

    private void BindCardCode()
    {
        gvCardCode.DataSource = CardCode.GetCardCodes();
        gvCardCode.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(Request["ID"]))
        {
            if (CardCode.Insert(new CardCodeInfo() { CadereEstablishment = Convert.ToInt32(CadereEstablishment.Text), ContractEstablishment = Convert.ToInt32(ContractEstablishment.Text), DepartmentID = ddlDepartmentList.SelectedValue, StartNum = StartCode.Text, EmStartNum = EmStartCode.Text, FrontCode = FronCode.Text, EmFrontCode = EmFronCode.Text, EndNum = EndCode.Text, EmEndNum = EmEndCode.Text, Establishment = Convert.ToInt32(Establishment.Text) }) > 0)
            {
                lblInfo.Text = "添加成功。";
                BindCardCode();
            }
            else
                lblInfo.Text = "添加失败！";
        }
        else
        {
            if (CardCode.Update(new CardCodeInfo() { CadereEstablishment = Convert.ToInt32(CadereEstablishment.Text), ContractEstablishment = Convert.ToInt32(ContractEstablishment.Text), DepartmentID = ddlDepartmentList.SelectedValue, StartNum = StartCode.Text, EmStartNum = EmStartCode.Text, FrontCode = FronCode.Text, EmFrontCode = EmFronCode.Text, EndNum = EndCode.Text, EmEndNum = EmEndCode.Text, Establishment = Convert.ToInt32(Establishment.Text), ID = Convert.ToInt32(hfID.Value) }))
            {
                lblInfo.Text = "修改成功。";
                BindCardCode();
            }
            else
                lblInfo.Text = "修改失败！";
        }
    }
    protected void gvCardCode_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (CardCode.Delete(int.Parse(gvCardCode.DataKeys[e.RowIndex].Value.ToString())))
        {
            lblInfo.Text = "删除成功。";
            BindCardCode();
        }
        else
            lblInfo.Text = "删除失败！";
    }
}
